﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class CMSMain : System.Web.UI.Page
{
    // Connection
    SqlConnection con = new SqlConnection("Data Source=RASIKA-PC;Initial Catalog=VLC_CMS;Integrated Security=True");
    SqlCommand cmd;
    string cmdStr;
    SqlDataReader dr;

    // Variables
    int checker;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    // Check for avaliability button - Student
    protected void btnUNAStu_Click(object sender, EventArgs e)
    {
        cmdStr = "SELECT USERNAME FROM STUDENT";
        con.Open();
        cmd = new SqlCommand(cmdStr, con);
        dr = cmd.ExecuteReader();
        
        checker = 0;
        while (dr.Read())
        {
            if (dr["USERNAME"].ToString().Equals(txtUsernameStu.Text)) checker = 1;
        }
        con.Close();

        if (checker == 1)
        {
            lblUNAStu.ForeColor = System.Drawing.Color.Red;
            lblUNAStu.Text = "You cannot have this username...!";
        }
        else
        {
            lblUNAStu.ForeColor = System.Drawing.Color.Green;
            lblUNAStu.Text = "You can have this username...!";
        }
    }

    // Check for avaliability button - Student
    protected void btnUNALec_Click(object sender, EventArgs e)
    {
        cmdStr = "SELECT USERNAME FROM LECTURER";
        con.Open();
        cmd = new SqlCommand(cmdStr, con);
        dr = cmd.ExecuteReader();

        checker = 0;
        while (dr.Read())
        {
            if (dr["USERNAME"].ToString().Equals(txtUsernameLec.Text)) checker = 1;
        }
        con.Close();

        if (checker == 1)
        {
            lblUNALec.ForeColor = System.Drawing.Color.Red;
            lblUNALec.Text = "You cannot have this username...!";
        }
        else
        {
            lblUNALec.ForeColor = System.Drawing.Color.Green;
            lblUNALec.Text = "You can have this username...!";
        }
    }

    // Submit button - Student
    protected void btnSubmitStu_Click(object sender, EventArgs e)
    {
        try
        {
            string name = txtNameStu.Text;
            string username = txtUsernameStu.Text;
            string password = txtPasswordStu.Text;
            string dob = txtDoB.Text;
            string gender = cmbGender.Text;
            string address = txtAddress.Text;
            string contactNo = txtContactNoStu.Text;
            string eMail = txtEmailStu.Text;
                      
            cmdStr = "INSERT INTO STUDENT(NAME, USERNAME, PASSWORD, DOB, GENDER, ADDRESS, CONTACT_NO, EMAIL) VALUES('" + name + "', '" + username + "', '" + password + "', '" + dob + "', '" + gender + "', '" + address + "', '" + contactNo + "',  '" + eMail + "')";
            con.Open();
            cmd = new SqlCommand(cmdStr, con);
            cmd.ExecuteNonQuery();
            con.Close();
            
            lblResultStu.Text = "Your registration is successful...!";      
        }
        catch (Exception ex)
        {
            lblResultStu.Text = ex.Message;
        }
    }

    // Submit button - Lecturer
    protected void btnSubmitLec_Click(object sender, EventArgs e)
    {
        try
        {
            string name = txtNameLec.Text;
            string username = txtUsernameLec.Text;
            string password = txtPasswordLec.Text;
            string qualifications = txtQualifications.Text;
            string contactNo = txtContactNoLec.Text;
            string eMail = txtEmailLec.Text;

            cmdStr = "INSERT INTO LECTURER(NAME, USERNAME, PASSWORD, QUALIFICATIONS, CONTACT_NO, EMAIL) VALUES('" + name + "', '" + username + "', '" + password + "', '" + qualifications + "', '" + contactNo + "',  '" + eMail + "')";
            con.Open();
            cmd = new SqlCommand(cmdStr, con);
            cmd.ExecuteNonQuery();
            con.Close();

            lblResultLec.Text = "Your registration is successful...!";
        }
        catch (Exception ex)
        {
            lblResultLec.Text = ex.Message;
        }
    }

    // Reset button - Student
    protected void btnResetStu_Click(object sender, EventArgs e)
    {
        txtNameStu.Text = "";
        txtUsernameStu.Text = "";
        txtPasswordStu.Text = "";
        txtDoB.Text = "";
        cmbGender.SelectedIndex = 0;
        txtAddress.Text = "";
        txtContactNoStu.Text = "";
        txtEmailStu.Text = "";

        lblUNAStu.Text = "";
        lblResultStu.Text = "";
    }

    // Reset button - Lecturer
    protected void btnResetLec_Click(object sender, EventArgs e)
    {
        txtNameLec.Text = "";
        txtUsernameLec.Text = "";
        txtPasswordLec.Text = "";
        txtQualifications.Text = "";
        txtContactNoLec.Text = "";
        txtEmailLec.Text = "";

        lblUNALec.Text = "";
        lblResultLec.Text = "";
    }
}